<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="style.css">
    <title>Document</title>
</head>
<body>
    <div class="book p3d">
        <!-- 右边 -->
        <div class="book-cover">

        </div>
        <!-- 左边 -->
        <div class="front-cover p3d">
            <div class="page front flip p3d">
                <p>
                    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam fermentum nisl quis nulla eleifend dignissim.Curabitur varius lobortis tincidunt. Maecenas gravida, nulla quis luctus imperdiet, ipsum nibh consectetur ante, in sodales massa tortor eget neque. Donec porta ligula massa, id sagittis est. Ut nisl tellus, faucibus nec feugiat ut, laoreet iaculis felis. Suspendisse ultrices mauris vel tellus suscipit commodo. Integer vitae tortor erat. Pellentesque non tempor nisi.
                </p>
            </div>
            <div class="page back"></div>
        </div>
    </div>

    <script>
        (function(window, document){
            var page = document.querySelector('.front-cover'),
                hold = false,
                centerPoint = window.innerWidth / 2,
                clamp = function(val, min, max) {
                    return Math.max(min, Math.min(val, max))
                };
            
            // 鼠标按住的时候
            window.onmousedown = function() {
                hold = true;
            }

           window.onmouseup = function() {
                hold = false;
            }
            
            // 翻书
            window.onmousemove = function(evt) {
                if(!hold) {
                    return
                }

                // 计算一个合理的旋转角度
                var angle = clamp((centerPoint - evt.pageX + 300) / 300 * -90, -180, 0)

                page.style.transform = `rotateY(${angle}deg)`
            }
            
        })(window, document)
    </script>
</body>
</html>
